package com.zeng.jiudian.servlet.hotel;

import com.alibaba.fastjson.JSONObject;
import com.zeng.jiudian.bean.HotelModel;
import com.zeng.jiudian.bean.PageModel;
import com.zeng.jiudian.service.HotelService;
import com.zeng.jiudian.service.impl.HotelServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author JJ_yo
 * @Date 2020/7/28 - 10:39
 */
@WebServlet("/back/hotel/findAll")
public class HotelListServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        String content = req.getParameter("content");
        String address = req.getParameter("address");
        String phone = req.getParameter("phone");
        String fax = req.getParameter("fax");
        String email = req.getParameter("email");
        String website = req.getParameter("website");
        String reservationPhone = req.getParameter("reservationPhone");
        String isShow = req.getParameter("isShow");
        String recommend = req.getParameter("recommend");
        String order = req.getParameter("order");
        String lift = req.getParameter("lift");
        String pageSize = req.getParameter("pageSize");
        String pageNum = req.getParameter("pageNum");
        String nowNum = req.getParameter("nowNum");

        HotelModel hotelModel = new HotelModel();
        hotelModel.setContent(content);
        hotelModel.setAddress(address);
        hotelModel.setPhone(phone);
        hotelModel.setFax(fax);
        hotelModel.setEmail(email);
        hotelModel.setWebsite(website);
        hotelModel.setReservationPhone(reservationPhone);
        hotelModel.setIsShow(isShow == null ? null : Integer.parseInt(isShow));
        hotelModel.setRecommend(recommend == null ? null : Integer.parseInt(recommend));
        hotelModel.setOrder(order);
        hotelModel.setLift(lift);

        PageModel pageModel = new PageModel();
        pageModel.setPageNum((pageNum == null || "".equals(pageNum) || "0".equals(pageNum)) ? 1 : Integer.parseInt(pageNum));
        pageModel.setPageSize((pageSize == null || "".equals(pageSize) || "0".equals(pageSize)) ? 10 : Integer.parseInt(pageSize));
        pageModel.setNowNum((nowNum == null || "".equals(nowNum) || "0".equals(nowNum)) ? 1 : Integer.parseInt(nowNum));
        hotelModel.setPageModel(pageModel);

        HotelService hotelService = new HotelServiceImpl();
        List<HotelModel> list = hotelService.findAll(hotelModel);
        int count = hotelService.getCount(hotelModel);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
    }
}
